#include "formpantilt.h"
#include "ui_formpantilt.h"



FormPanTilt::FormPanTilt(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FormPanTilt)
{
    setFocusPolicy(Qt::StrongFocus);    //this is needed to get all keyboard inputs
    ui->setupUi(this);


    //move this to FORM SHOW
    //handle errors or something ?
    //errCode = ioctl(fileHandle, IOCTL_PANTILT_RESET, &OutputValue);
    ioctl(fileHandle, IOCTL_PANTILT_RESET);   //reset at start-up

    //verify error and quit if doesn't work ?
    //init position
    PositionX = 0;
    PositionY = 0;
    //init scroll bars (range + value)
    ui->horizontalScrollBar->setMinimum(-HORIZONTAL_RANGE);
    ui->horizontalScrollBar->setMaximum(HORIZONTAL_RANGE);
    ui->horizontalScrollBar->setValue(0);
    ui->verticalScrollBar->setMinimum(-VERTICAL_RANGE);
    ui->verticalScrollBar->setMaximum(VERTICAL_RANGE);
    ui->verticalScrollBar->setValue(0);
    MoveCursor();
}

FormPanTilt::~FormPanTilt()
{
    delete ui;
}

void FormPanTilt::setFileHandle(int file)
{
    if (file > 0)
        fileHandle = file;
}

int FormPanTilt::MoveX(int newPos)
{
    int finalPosition;
    finalPosition = PositionX;
    if (newPos > PositionX) //is the if case really useful since checked in while loop ?
    {
        while(newPos > PositionX)
        {
            if (ioctl(fileHandle, IOCTL_PANTILT, MOVE_LEFT)>=0)
                finalPosition++;
            newPos--;
        }
    }
    else if (newPos < PositionX)
    {
        while(newPos < PositionX)
        {
            if (ioctl(fileHandle, IOCTL_PANTILT, MOVE_RIGHT)>=0)
                finalPosition--;
            newPos++;
        }
    }
    return finalPosition;
}

int FormPanTilt::MoveY(int newPos)
{
    int finalPosition;
    finalPosition = PositionY;
    if (newPos > PositionY) //is the if case really useful since checked in while loop ?
    {
        while(newPos > PositionY)
        {
            if (ioctl(fileHandle, IOCTL_PANTILT, MOVE_DOWN)>=0)
                finalPosition++;
            newPos--;
        }
    }
    else if (newPos < PositionY)
    {
        while(newPos < PositionY)
        {
            if (ioctl(fileHandle, IOCTL_PANTILT, MOVE_UP)>=0)
                finalPosition--;
            newPos++;
        }
    }
    return finalPosition;
}

void FormPanTilt::MoveCursor()
{
    int x;  //horizontal position
    int y;  //vertical
    //start with horizontal Position
    //get container width
    x = ui->FramePosition->width();
    //get value range from scrollbar
    x /= (ui->horizontalScrollBar->maximum() - ui->horizontalScrollBar->minimum()) + 1;
    //move along with scrollbar index
    x *= PositionX;
    //place on frame (note: cursor is already on frame, no need to compute offset)
    x += ui->FramePosition->width()/2 - ui->PositionCursor->width()/2;
    //do the same for vertical position
    y = ui->FramePosition->height();
    y /= (ui->verticalScrollBar->maximum() - ui->verticalScrollBar->minimum()) + 1;
    y *= PositionY;
    y += ui->FramePosition->height()/2 - ui->PositionCursor->height()/2;
    //now, actually move the cursor
    ui->PositionCursor->move(x,y);
    //update the labels with new position
    UpdateLabels();
}

void FormPanTilt::UpdateLabels()
{
    QString tmpStr;
    int invertedY;
    //X
    tmpStr.setNum(PositionX);
    if (PositionX > 0)  //if it's positive, add "+" prefix
        tmpStr = ("+" + tmpStr);
    ui->LabelPosX->setText(tmpStr);
    //Y
    invertedY = (-1) * PositionY;
    tmpStr.setNum(invertedY);
    if (invertedY > 0)
        tmpStr = ("+" + tmpStr);
    ui->LabelPosY->setText(tmpStr);
}

void FormPanTilt::on_horizontalScrollBar_valueChanged(int value)
{
    //horizontal movement... MOVE CAM...
    PositionX = MoveX(value);
    MoveCursor();
    ui->horizontalScrollBar->setValue(PositionX);
}

void FormPanTilt::on_verticalScrollBar_valueChanged(int value)
{
    //vertical movement... MOVE CAM...
    PositionY = MoveY(value);
    MoveCursor();
    ui->verticalScrollBar->setValue(PositionY);
}

void FormPanTilt::on_PushButtonReset_clicked()
{
    ioctl(fileHandle, IOCTL_PANTILT_RESET);   //reset at start-up
    PositionX = 0;
    PositionY = 0;
    MoveCursor();
    ui->horizontalScrollBar->setValue(0);
    ui->verticalScrollBar->setValue(0);
}

void FormPanTilt::keyPressEvent( QKeyEvent *k )
{
    int tmpValue;

    switch (k->key())
    {
        case (Qt::Key_Up):
        {
            tmpValue = ui->verticalScrollBar->value();
            if (tmpValue > -VERTICAL_RANGE)
                ui->verticalScrollBar->setValue(--tmpValue);
            break;
        }
        case (Qt::Key_Down):
        {
            tmpValue = ui->verticalScrollBar->value();
            if (tmpValue < VERTICAL_RANGE)
                ui->verticalScrollBar->setValue(++tmpValue);
            break;
        }
        case (Qt::Key_Right):
        {
            tmpValue = ui->horizontalScrollBar->value();
            if (tmpValue < HORIZONTAL_RANGE)
                ui->horizontalScrollBar->setValue(++tmpValue);
            break;
        }
        case (Qt::Key_Left):
        {
            tmpValue = ui->horizontalScrollBar->value();
            if (tmpValue > -HORIZONTAL_RANGE)
                ui->horizontalScrollBar->setValue(--tmpValue);
            break;
        }
        default:
            break;
    }
}
